home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- Newsgroups: comp.lang.c++
- Subject: Re: type-casting in multi-inheritance
- Date: Mon, 12 Feb 1996 09:31:31 +0100
- Organization: Fachbereich Informatik, TH Darmstadt
- Message-ID: <311EFAE3.41C67EA6@intellektik.informatik.th-darmstadt.de>
- References: <DMn7Ix.G9@Virginia.EDU>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
-
- Gregory Carl Lewin wrote:
- >
- > I am a bit fuzzy about casting from pointers to derived objects
- > to base classes when you are using multiple inheritance.
- > Perhaps someone could explain the following to me. Consider
- > the arrangement:
- >
- > class Base1 {};
- > class Base2 {};
- > class Derived12 : public Base1, Base2 {};
- >
- > Base1 b1;
- > Base2 b2;
- > Derived12 d12;
- >
- > void foo(void) {
- > Base1* pb1;
- > pb1=&b1;
- > pb1=&d12;
- >
- > Base2* pb2;
- > pb2=&b2;
- > pb2=&d12; //ERROR: Cannot convert pointer
- > pb2=(Base2*) &d12; //OK: But is it ALWAYS safe?
- >
- > }
- >
- > Because Derived 12 is publicly derived from Base1 and Base2, I
- > would have expected the conversion of &d12 to a Base2* to be
- > automatic; however, the comiler balks. The subsequent explicit
- > conversion works, and the program seems to act appropriately,
- > but using the explicit type-cast scares me. Can someone
- > explain why this is and if this is the "correct" way around the
- > problem (alternatives being virtual inheritance with class Base
- > being a base for Base1 and Base2, and ???).
- >
-
- 'Derived12' inherits _private_ not public from 'Base2'.
- The correct definition of 'Derived12' should be
-
- class Derived12 : public Base1, public Base2 {};
- ^^^^^^
-
- Now the problem should disappear.
-
- Enno
-